# 歌单地址 ：https://music.163.com/discover/toplist?id=3779629
import requests
import re
import os
music_url_list = 'https://music.163.com/discover/toplist?id=3779629'
filepath ='music'
if not os.path.exists(filepath):
    os.makedirs(filepath)
header = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
}
res = requests.get(music_url_list, headers=header)
res.encoding = res.apparent_encoding
# print(res.text)
song_data = re.findall(r'<li><a href="/song\?id=(\d+)">(.*?)</a></li>', res.text)
for song_id, song_name in song_data:
    music_url = 'http://music.163.com/song/media/outer/url?id={}.mp3'.format(song_id)
    with open(f'{filepath}/{song_name}.mp3', 'wb') as f:
        f.write(requests.get(music_url, headers=header).content)
        print(song_name,'----> 下载完成~~~~')
    exit(-1)

